﻿(function ($) {
    $.login = {
        formMessage: function (msg) {
            $('.login_tips').find('.tips_msg').remove();
            $('.login_tips').append('<div class="tips_msg">' + msg + '</div>');
        },
        loginClick: function () {
            var $username = $("#txt_account");
            var $password = $("#txt_password");
            var $code = $("#txt_code");
            if ($username.val() == "") {
                $username.focus();
                $.login.formMessage('请输入用户名!');
                return false;
            } else if ($password.val() == "") {
                $password.focus();
                $.login.formMessage('请输入登录密码!');
                return false;
            } else if ($code.val() == "") {
                $code.focus();
                $.login.formMessage('请输入验证码。');
                return false;
            } else {
                $("#login_button").attr('disabled', 'disabled').find('span').html("loading...");
                $.ajax({
                    url: "/Admin/Login/CheckLogin",
                    data: { username: $.trim($username.val()), password: $.md5($.trim($password.val())), code: $.trim($code.val()) },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.state == "success") {
                            $("#login_button").find('span').html("登录成功，正在跳转...");
                            window.setTimeout(function () {
                                window.location.href = "/Admin/Home/Index";
                            }, 100);
                        } else {
                            $("#login_button").removeAttr('disabled').find('span').html("登录");
                            $("#switchCode").trigger("click");
                            $code.val('');
                            $.login.formMessage(data.message);
                        }
                    }
                });
            }
        },
        init: function () {
            $('.wrapper').height($(window).height());
            $(".container").css("margin-top", ($(window).height() - $(".container").height()) / 2 - 50);
            $(window).resize(function (e) {
                $('.wrapper').height($(window).height());
                $(".container").css("margin-top", ($(window).height() - $(".container").height()) / 2 - 50);
            });

            $("#imgcode").click(function () {
                $("#imgcode").attr("src", "/Admin/Login/GetAuthCode?time=" + Math.random());
            });
            $("#switchCode").click(function () {
                $("#imgcode").attr("src", "/Admin/Login/GetAuthCode?time=" + Math.random());
            });
            $("#switchRegCode").click(function () {
                $("#regImgcode").attr("src", "/Admin/Login/GetRegCode?time=" + Math.random());
            });
            $("#regImgcode").click(function () {
                $("#regImgcode").attr("src", "/Admin/Login/GetRegCode?time=" + Math.random());
            });

            var login_error = top.$.cookie('nfine_login_error');
            if (login_error != null) {
                switch (login_error) {
                    case "overdue":
                        $.login.formMessage("系统登录已超时,请重新登录");
                        break;
                    case "OnLine":
                        $.login.formMessage("您的帐号已在其它地方登录,请重新登录");
                        break;
                    case "-1":
                        $.login.formMessage("系统未知错误,请重新登录");
                        break;
                }
                top.$.cookie('nfine_login_error', '', { path: "/", expires: -1 });
            }
            $("#login_button").click(function () {
                $.login.loginClick();
            });
            document.onkeydown = function (e) {
                if (!e) e = window.event;
                if ((e.keyCode || e.which) == 13) {
                    document.getElementById("login_button").focus();
                    document.getElementById("login_button").click();
                }
            }
        },
        initCustsvr: function () {
            $(".suspend").mouseover(function () {
                $(this).stop();
                $(this).animate({ width: 160 }, 200);
            });
            $(".suspend").mouseout(function () {
                $(this).stop();
                $(this).animate({ width: 40 }, 200);
            });
        }
    };


    var count = 60; //间隔函数，1秒执行
    var InterValObj1; //timer变量，控制时间
    var curCount1;//当前剩余秒数  
    $.Register = {
        errMsg: function (msg) {
            $('.reg_tips').find('.tips_msg').remove();
            $('.reg_tips').append('<div class="tips_msg">' + msg + '</div>');
        },
        isMobilePhone: function (value) {
            return !(value.search(/(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/) == -1);
        },
        sendMessage:function(){
            curCount1 = count;
            var phone = $.trim($('#telNo').val());
            if (!$.Register.isMobilePhone(phone)) {
                $.Register.errMsg("请输入有效的手机号!");
                return false;
            }
            var verifyCode = $("#txt_VerifyCode").val();
            if (verifyCode == '' || verifyCode.length <4) {
                $.Register.errMsg("请输入正确的验证码!");
                return false;
            }

            //设置button效果，开始计时
            $("#btnSendCode").attr("disabled", "true");
            $("#btnSendCode").val(+curCount1 + "秒再获取");
            InterValObj1 = window.setInterval($.Register.setRemainTime, 1000); //启动计时器，1秒执行一次          

            $.ajax({
                url: "/Admin/Login/SendSMSCode",
                type: "POST",
                data: { mobileNo: phone, verifyCode: verifyCode },
                dataType: "json",
                success: function (ret) {
                    if (ret != null) {
                        layer.alert(data.message, { icon: 2, time: 300000 });
                        //$.Register.errMsg(ret.message);
                    }
                    $("#regImgcode").trigger("click");//验证码重置
                }
            });
            //向后台发送处理数据
        },
        setRemainTime: function () {
            if (curCount1 == 0) {
                window.clearInterval(InterValObj1);//停止计时器
                $("#btnSendCode").removeAttr("disabled");//启用按钮
                $("#btnSendCode").val("重新发送");
            }
            else {
                curCount1--;
                $("#btnSendCode").val(+curCount1 + "秒再获取");
            }
        },
        checkRegisterVal: function (regData) {
            if (regData.telNo != "" && regData.password != "" && regData.nickNm != "") {
                if (regData.verifyCode == '') {
                    $.Register.errMsg("请输入验证码!");
                    return false;
                }
                if (!$.Register.isMobilePhone(regData.telNo)) {
                    $.Register.errMsg("请输入有效的手机号!");
                    return false;
                }
                if (regData.password.length < 6) {
                    $.Register.errMsg("请输入6位以上密码!");
                    return false;
                }

                var confirmPwd = $("#confirmPwd").val();
                if (regData.password != confirmPwd) {
                    $.Register.errMsg("两次输入的密码不一致，请确认后重试!");
                    return false;
                }
                return true;
            }
            else {
                $.Register.errMsg("请将注册信息填写完整!");
                return false;
            }
        },
        regClick: function () {
            var regModel = {};
            regModel.telNo = $("#telNo").val();
            regModel.password = $("#pwd").val();
            regModel.smsCode = $("#smsCode").val();
            regModel.nickNm = $("#nickName").val();
            if ($.Register.checkRegisterVal(regModel)) {
                regModel.password = $.md5($.trim(regModel.password));
                $("#registerUser").attr('disabled', 'disabled')
                $.ajax({
                    url: "/Admin/Login/Register",
                    type: "POST",
                    data: regModel,
                    dataType: "json",
                    success: function (ret) {
                        if (ret != null) {
                            if (ret.state == "success") {
                                $("#registerUser").find('span').html("注册成功，快去登录体验吧...");
                                window.setTimeout(function () {
                                    window.location.href = "/Admin/Home/Index";
                                }, 1000);
                            } else {
                                $("#registerUser").removeAttr('disabled');
                                //刷新验证码
                                $("#switchRegCode").trigger("click");
                                $.Register.errMsg(ret.message);
                            }
                        }
                    }
                });
            } else {
                return false;
            }
        }
    };
   

    $(function () {
        $.login.init();
        $.login.initCustsvr();

        $("#registerUser").click(function () {
            $.Register.regClick();
            return false;
        })

        $("#btnSendCode").click(function () {
            $.Register.sendMessage();
            return false;
        })

        $('.message a').click(function () {
            $('form').animate({
                height: 'toggle',
                opacity: 'toggle'
            }, 'slow');
        });
    });
})(jQuery);